package com.ibeeking.found.file.service.common.util;

import com.ibeeking.nematos.utils.data.Collections3;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class DoUtils {

    /**
     * 检查重名或编码重复
     * @param t
     * @param mapper
     * @param id
     * @param name
     * @param code
     * @param <T>
     * @param <M>
     * @return
     */
    public static  <T , M extends BaseMapper<T>> List<T> getRepeatList(T t , M mapper, Long id, String name, String code) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(id != null, "id", id)
                .and(wrapper -> wrapper.eq(StringUtils.isNotBlank(name), "name", name)
                        .or()
                        .eq(StringUtils.isNotBlank(code), "code", code));
        return mapper.selectList(queryWrapper);
    }

    public static  <T , M extends BaseMapper<T>> Boolean checkRepeat(T t , M mapper, Long id, String name, String code) {
        List<T> result = DoUtils.getRepeatList(t,mapper,id,name,code);
        if (Collections3.isNotEmpty(result)) {
            return true;
        }
        return false;
    }

}
